/*
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2014
 *
 * fileName:  MultiActionControllerAdapter.java
 * author:  lei
 * Company:  Cloudland Studio
 * Createdate:  2013-9-15 上午8:47:36
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2013-9-15          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.web.multipart;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.cloudland.util.JsonResolve;
import org.cloudland.web.context.HttpHandle;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 {@link java.lang.Object#toString()}, 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  MultiActionControllerAdapter
 * @author  lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class MultiActionControllerAdapter extends MultiActionController {

	 /**
     * log 日志对象
     */
    private final Logger log = Logger.getLogger(this.getClass());
    
    /** 
     * 获取日志对象
     *
     * @return 
     */
    protected Logger getLogger() {
        return log;
    }
    
    /** 
     * 将映射关系设置到父类中
     * @param mappings URL和Action的映射关系 
     */
    public void setMappings(Properties mappings) {
        PropertiesMethodNameResolver resolver = new PropertiesMethodNameResolver();
        resolver.setMappings(mappings);
        setMethodNameResolver(resolver);
    }
    
    /** 
     * 将对象转换为 Json 数据格式返回页面。<br>
     * <pre>
     * 默认UTF-8编码输出，一般用于JSON字符串的输出，可避免中文乱码问题；
     * 经过此方法将把对象封装为：
     *  {'code':'1', value:{xxx}, errMsg:''}
     *  code:响应码表示是否处理成功。1:成功, 0:失败；
     *  value:为Java对象转换的Json格式数据；
     *  errMsg:如果响应码为0，可以从errMsg中获取错误信息 
     * </pre>
     *
     * @param errMsg 错误信息，为空表示处理正确。
     * @param result 需要转换为 Json 数据对象并返回页面的 Java 对象
     * @param response Http响应对象
     */
    protected void toJson(Object result, String errMsg) {

        JsonResponse json = new JsonResponse();
        json.setValue(result);
        if (null != errMsg && errMsg.length() > 0) {
            json.setCode("0");
            json.setErrMsg(errMsg);
        } else {
            json.setCode("1");
        }
        
        String resultJosn = JsonResolve.toJson(json);
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Result Json [" + resultJosn + "]");
        }
        
        BufferedWriter writer=null;
        try {
            OutputStream out= HttpHandle.getInstance().getResponse().getOutputStream();
            OutputStreamWriter ow=new OutputStreamWriter(out, "UTF-8");
            writer=new BufferedWriter(ow);
            writer.write(resultJosn);
            writer.flush();
        } catch (UnsupportedEncodingException e) {
            getLogger().error("");
        } catch (IOException e) {
            getLogger().error("");
        }finally{
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    
                }
            }
        }
    }
    
    /**
     * Json页面返回封装数据
     * @ClassName JsonResponse
     * @Description TODO
     * @author zhuzhonglei
     * @date 2013-2-20 下午2:59:58
     * @version 1.0
     *
     */
    public static class JsonResponse {
        
        /**
         * code 返回码
         */
        private String code;
        
        /**
         * value 返回值
         */
        private Object value;
        
        /**
         * errMsg 错误信息
         */
        private String errMsg;
        
        public JsonResponse(){}

        public JsonResponse(String code, Object obj, String errMsg) {
            this.code = code;
            this.value = obj;
            this.errMsg = errMsg;
        }

        public String getCode()
        {
            return this.code;
        }

        public void setCode(String code)
        {
            this.code = code;
        }


        public String getErrMsg()
        {
            return errMsg;
        }

        public void setErrMsg(String errMsg)
        {
            this.errMsg = errMsg;
        }

        public Object getValue()
        {
            return value;
        }

        public void setValue(Object value)
        {
            this.value = value;
        }
    }
}
